//
//  MXUserViewController.m
//  Stratups
//
//  Created by 王启颖 on 2019/2/27.
//  Copyright © 2019 王启颖. All rights reserved.
//

#import "MXUserViewController.h"
#import "MXUserTableView.h"
#import "MXCountView.h"
#import "MXClockThisWeekModel.h"
#import "MXUserHeadView.h"

@interface MXUserViewController ()
@property (strong,nonatomic) MXUserTableView *userTableView;
@property (strong,nonatomic) MXCountView *countView;
@property (strong,nonatomic) MXUserHeadView *userHeadView;
@property (strong,nonatomic) UIView *headView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (strong, nonatomic) NSMutableDictionary *dicParams;
@end

@implementation MXUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self userTableView];
    [self getClockThisWeekRequest];
    
}

#pragma mark - 懒加载 -
-(MXUserHeadView *)userHeadView{
    if (!_userHeadView) {
        MXUserHeadView *view = [MXUserHeadView initMXUserHeadView];
        view.frame = CGRectMake(0, 0, kScreenWidth, 160);
        _userHeadView = view;
        _userHeadView.user = self.user;
        
    }
    return _userHeadView;
}

-(MXUserTableView *)userTableView{
    if (!_userTableView) {
        
        MXUserTableView *view = [[MXUserTableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - KSafeAreaBottonHeight - KSafeAreaTopHeight)];
        _userTableView = view;
        _userTableView.user_id = self.user.user_id;
        _userTableView.tableView.tableHeaderView = self.headView;
        [self.view addSubview:_userTableView];
    }
    return _userTableView;
}

-(MXCountView *)countView{
    if (!_countView) {
        
        MXCountView *view = [MXCountView initMXCountView];
        view.frame = CGRectMake(0,_userHeadView.height, kScreenWidth, 125);
        _countView = view;
        
    }
    return _countView;
}

-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 295)];
        [_headView addSubview:self.userHeadView];
        [_headView addSubview:self.countView];
    }
    return _headView;
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"user_id"] = self.user.user_id;
    
}
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

#pragma mark - 网络请求 -
-(void)getClockThisWeekRequest{
    [SVProgressHUD showWithStatus:@"正在检测打卡。。。"];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getClockThisWeek params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) {
            NSLog(@"%@",result);
            
            for (id res in result[@"message"]) {
                MXClockThisWeekModel *model = [MXClockThisWeekModel mj_objectWithKeyValues:res];
                NSInteger dateWeek = [NSString dateWeekWithDateString:[NSString stringWithFormat:@"%ld",model.created_at/1000]];
                
                
                [self.paramsArr addObject:[NSNumber numberWithInteger:dateWeek]];
            }
            

            self.countView.paramsArr = self.paramsArr;
            
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:result[@"message"]];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}

-(BOOL)isClock{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    NSInteger today = [NSString dateWeekWithDateString:timeSp];
    
    for (NSNumber *time in self.paramsArr) {
        if ( [time integerValue] == today) {
            return true;
        }
    }
    
    return false;
    
}


@end
